package com.tansun.ruledata.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tansun.rule.config.AppConfiguration;
import com.tansun.rule.pojo.EnterPoint;
import com.tansun.rule.pojo.User;
import com.tansun.ruledata.data.model.ProjectEnterPoint;
import com.tansun.ruledata.data.service.RuleResourceLoadService;

@Controller
@RequestMapping(value = "/RuleDataProject")
public class RuleDataProjectController {
	
	@Autowired
	private  RuleResourceLoadService  ruleResourceLoadService;
	
	
	@RequestMapping("/list.do")
	public String projectlist(HttpServletRequest request, ModelMap map){
		User cUser = (User)request.getSession().getAttribute("currentUser");

		List<ProjectEnterPoint> ps = new ArrayList<ProjectEnterPoint>();
		
		ProjectEnterPoint  projectEnterPoint = null;
		EnterPoint  enterPoint = null;
		if(cUser.getUserName().equals("admin")){
			Set<String> set = AppConfiguration.ruleEnterMap.keySet();
			for (String str : set) {
				Map<String,EnterPoint>  pMap  = AppConfiguration.ruleEnterMap.get(str);
				if(pMap==null)
					continue;
				for (String key :pMap.keySet()){
					enterPoint = pMap.get(key);
					projectEnterPoint = new ProjectEnterPoint();
					projectEnterPoint.setControl(enterPoint.getControl());
					projectEnterPoint.setEnterPointCode(enterPoint.getCode());
					projectEnterPoint.setProjectCode(str);
					projectEnterPoint.setRuleflowId(enterPoint.getRuleflowId());
					projectEnterPoint.setUpdateDate(enterPoint.getUpdateDate());
					ps.add(projectEnterPoint);
				}
			
			}
		}
		map.put("cProjs", ps);
		return "rulecontrol/ruleControl";
	}
	
	@RequestMapping("/showdrl.do")
	public String showdrl(HttpServletRequest request, ModelMap map){
		String   pointcode  = request.getParameter("pointcode");

		if (pointcode !=null ){
		  List<String>  list =	AppConfiguration.deployDrlList.get(pointcode);
		  map.put("drlList", list);
		}
		
		return  "rulecontrol/dialog";
	}
	
	@ResponseBody
	@RequestMapping("/reStart.do")
	public String reStart(String projectCode,String  pointCode, ModelMap map){
		if (projectCode != null && pointCode != null){
			this.ruleResourceLoadService.loadEnterPoint(projectCode, pointCode);
			try {
				this.ruleResourceLoadService.loadEnterPointDeployResource(projectCode, pointCode);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return  "0";
			}
		}
		
		return  "1";
	}
	

}
